const {
    excludeFields,
    wait,
    dateFormat,
    toString,
    arrayToMap,
    stringValidator: {
        isEmail,
    },
    CombinedPredicate: {
        not,
    },
} = rx

const profileValidator = RxFormValidator.validator({
    rxElemValidators: [
        RxElemValidator.notBlank('regMail', '[认证邮箱] 不能为空'),
        RxElemValidator.notBlank('newPassword', '[密码] 不能为空'),
        RxElemValidator.notBlank('passwordConfirm', '[确认密码] 不能为空'),

        RxElemValidator.limitLen('regMail', '[认证邮箱] 长度最大为 128 位', 128),
        RxElemValidator.limitLen('password', '[密码] 长度最大为 64 位', 64),
        RxElemValidator.limitLen('passwordConfirm', '[确认密码] 长度最大为 64 位', 64),

        RxElemValidator.create('password', '[密码] 与 [确认密码] 不一致', (_v, {newPassword, passwordConfirm}) => newPassword !== passwordConfirm),
        RxElemValidator.create('regMail', '[邮箱] 格式错误', not(isEmail)),
    ],
})

/**
 * @author rxliuli
 */
const app = new Vue({
    el: '#app',
    data() {
        return {
            activeName:"mode1",
            tab: 1,
            sysUser: new SysUser({
                newPassword: undefined,
                passwordConfirm: undefined,

            }),
            form: new SysUser({
                includeSysMenuList: [],
                notIncludeSysMenuList: [],
            }),
            roles: [],
            sysUserRoleList: [],
            sysUserRoleMap: new Map(),
            roleIdList:[],
            includeSysMenuSelectList: [],
            notIncludeSysMenuSelectList: [],
            oldForm: new SysUser({
                includeSysMenuList: [],
                notIncludeSysMenuList: [],
            }),
        }
    },
    methods: {
        async deleteSysUser() {
            if (await rxPrompt.confirm('确定要删除么？', ['确定', '取消']) && await ajaxOption({
                res: sysUserApi.delete(this.sysUser.id),
                danger: '删除失败',
                success: '删除成功, 即将重新加载',
            })) {
                await
                    forward('system-config/admin_privilege')
            }
        },
        getTabClass(tab) {
            return this.tab === tab ? 'ho' : ''
        },
        changeActiveTab(tab) {
            this.tab = tab
        },
        getTabShow(tab) {
            return this.tab === tab ? '' : 'hide'
        },
        async updateSysUserOfPassword() {
            const {id, regMail, newPassword, passwordConfirm} = this.sysUser
            if (await profileValidator(this.sysUser) && await ajaxOption({
                res: sysUserApi.update({
                    id: id,
                    regMail,
                    password: newPassword,
                }),
                danger: '修改密码失败，请稍后再试',
                success: '修改密码成功',
            })) {
                this.sysUser = excludeFields(this.sysUser, 'newPassword', 'passwordConfirm')
                forward("/system-config/admin_privilege_admin_list");
            }
        },
        async updateSysUser(sysUser) {
            sysUser.roleIdList = this.roleIdList;
            if (await ajaxOption({
                res: sysUserApi.update({
                    id: this.sysUser.id,
                    ...sysUser,
                }),
                danger: '更新失败，请稍后再试',
                success: '更新成功',
            })) {
                this.sysUser = {...this.sysUser, ...sysUser}
                sysLoginApi.currentUser.clear()
                forward("/system-config/admin_privilege_admin_list");
            }
        },
        dateFormat(date) {
            return dateFormat(new Date(date), 'yyyy-MM-dd hh:mm:ss')
        },
        addSysMenuList() {
            this.form.includeSysMenuList.push(...this.notIncludeSysMenuSelectList)
            this.form.notIncludeSysMenuList = rx.filterItems(this.form.notIncludeSysMenuList, this.notIncludeSysMenuSelectList)
        },
        deleteSysMenuList() {
            this.form.includeSysMenuList = rx.filterItems(this.form.includeSysMenuList, this.includeSysMenuSelectList)
            this.form.notIncludeSysMenuList.push(...this.includeSysMenuSelectList)
        },
        async updateUserMenu() {
            const {left, right} = rx.diffBy(this.form.includeSysMenuList, this.oldForm.includeSysMenuList, ({id}) => id)
            if (await ajaxOption({
                res: sysUserApi.updateUserMenu(left.map(({id}) => ({
                    menuId: id,
                    userId: this.sysUser.id,
                })), right.map(({id}) => ({
                    menuId: id,
                    userId: this.sysUser.id,
                }))),
                danger: '修改权限失败，请稍后重试',
                success: '修改权限成功',
            })) {
                this.oldForm.includeSysMenuList = [...this.form.includeSysMenuList]
                this.oldForm.notIncludeSysMenuList = [...this.form.notIncludeSysMenuList]
                sysMenuApi.loadSysMenuList.clear()
            }
        },
    },
    computed: {
    },
    async mounted() {
        this.sysUser = forward.getData()
        this.oldForm = _.merge(this.form, await sysUserApi.getUserMenu(forward.getData().id))
        this.form = _.cloneDeep(this.oldForm)
        this.roles = await sysRoleApi.selectList()
        this.sysUserRoleList = await sysUserRoleApi.getByUserId(this.sysUser.id)
        this.sysUserRoleList.forEach(sur => this.roleIdList.push(sur.roleId))
    },
})
